Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
braces
Advanced tools
Package description
The braces npm package is a library for expanding braces in a string, similar to how shells like bash perform brace expansion. It is used to generate permutations of strings based on patterns with braces, which is useful for file globbing, configuration, and other tasks where such patterns are needed.
Brace Expansion
Expands a pattern with comma-separated values inside braces into an array of strings.
"{a,b,c}d" -> ['ad', 'bd', 'cd']
Sequence Expansion
Expands a pattern with numerical sequences inside braces into an array of strings.
"{1..3}d" -> ['1d', '2d', '3d']
Nested Expansion
Expands a pattern with nested braces into an array of strings.
"{a,{b,c}d}e" -> ['ae', 'bde', 'cde']
Minimatch is a minimal matching utility that works by converting glob expressions into JavaScript RegExp objects. It is similar to braces in that it supports pattern matching, but it is more focused on matching file paths and does not support brace expansion.
Micromatch is a fast and accurate glob matching library, optimized for speed and accuracy. It extends the capabilities of minimatch and braces, providing more powerful matching features and better performance for complex patterns.
Globby is a user-friendly glob matching library that uses micromatch under the hood. It provides a higher-level API for file matching and supports multiple patterns, making it a convenient alternative to braces for file system operations.
Changelog
[1.8.5] - 2016-05-21
Readme
Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces specification.
Install with npm:
$ npm install braces --save
a/{b,c}/d
=> ['a/b/d', 'a/c/d']
{1..3}
=> ['1', '2', '3']
var braces = require('braces');
braces('a/{x,y}/c{d}e')
//=> ['a/x/cde', 'a/y/cde']
braces('a/b/c/{x,y}')
//=> ['a/b/c/x', 'a/b/c/y']
braces('a/{x,{1..5},y}/c{d}e')
//=> ['a/x/cde', 'a/1/cde', 'a/y/cde', 'a/2/cde', 'a/3/cde', 'a/4/cde', 'a/5/cde']
Use braces to generate test fixtures!
Example
var braces = require('./');
var path = require('path');
var fs = require('fs');
braces('blah/{a..z}.js').forEach(function(fp) {
if (!fs.existsSync(path.dirname(fp))) {
fs.mkdirSync(path.dirname(fp));
}
fs.writeFileSync(fp, '');
});
See the tests for more examples and use cases (also see the bash spec tests);
Uses expand-range for range expansion.
braces('a{1..3}b')
//=> ['a1b', 'a2b', 'a3b']
braces('a{5..8}b')
//=> ['a5b', 'a6b', 'a7b', 'a8b']
braces('a{00..05}b')
//=> ['a00b', 'a01b', 'a02b', 'a03b', 'a04b', 'a05b']
braces('a{01..03}b')
//=> ['a01b', 'a02b', 'a03b']
braces('a{000..005}b')
//=> ['a000b', 'a001b', 'a002b', 'a003b', 'a004b', 'a005b']
braces('a{a..e}b')
//=> ['aab', 'abb', 'acb', 'adb', 'aeb']
braces('a{A..E}b')
//=> ['aAb', 'aBb', 'aCb', 'aDb', 'aEb']
Pass a function as the last argument to customize range expansions:
var range = braces('x{a..e}y', function (str, i) {
return String.fromCharCode(str) + i;
});
console.log(range);
//=> ['xa0y', 'xb1y', 'xc2y', 'xd3y', 'xe4y']
See expand-range for benchmarks, tests and the full list of range expansion features.
Type: Boolean
Deafault: false
Return a regex-optimal string. If you're using braces to generate regex, this will result in dramatically faster performance.
Examples
With the default settings ({makeRe: false}
):
braces('{1..5}');
//=> ['1', '2', '3', '4', '5']
With {makeRe: true}
:
braces('{1..5}', {makeRe: true});
//=> ['[1-5]']
braces('{3..9..3}', {makeRe: true});
//=> ['(3|6|9)']
Type: Boolean
Default: false
Enables complete support for the Bash specification. The downside is a 20-25% speed decrease.
Example
Using the default setting ({bash: false}
):
braces('a{b}c');
//=> ['abc']
In bash (and minimatch), braces with one item are not expanded. To get the same result with braces, set {bash: true}
:
braces('a{b}c', {bash: true});
//=> ['a{b}c']
Type: Boolean
Deafault: true
Duplicates are removed by default. To keep duplicates, pass {nodupes: false}
on the options
Better support for Bash 4.3 than minimatch
This project has comprehensive unit tests, including tests coverted from Bash 4.3. Currently only 8 of 102 unit tests fail, and
Install dev dependencies:
npm i -d && npm benchmark
#1: escape.js
brace-expansion.js x 114,934 ops/sec ±1.24% (93 runs sampled)
braces.js x 342,254 ops/sec ±0.84% (90 runs sampled)
#2: exponent.js
brace-expansion.js x 12,359 ops/sec ±0.86% (96 runs sampled)
braces.js x 20,389 ops/sec ±0.71% (97 runs sampled)
#3: multiple.js
brace-expansion.js x 114,469 ops/sec ±1.44% (94 runs sampled)
braces.js x 401,621 ops/sec ±0.87% (91 runs sampled)
#4: nested.js
brace-expansion.js x 102,769 ops/sec ±1.55% (92 runs sampled)
braces.js x 314,088 ops/sec ±0.71% (98 runs sampled)
#5: normal.js
brace-expansion.js x 157,577 ops/sec ±1.65% (91 runs sampled)
braces.js x 1,115,950 ops/sec ±0.74% (94 runs sampled)
#6: range.js
brace-expansion.js x 138,822 ops/sec ±1.71% (91 runs sampled)
braces.js x 1,108,353 ops/sec ±0.85% (94 runs sampled)
You might also be interested in these projects:
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on May 21, 2016.
FAQs
Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed.
We found that braces demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).